package com.heima.article.listener;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.WmNewsMessageConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author TheCai
 * @date 2021/6/3 21:21
 */
@Component
@Slf4j
public class ArticleIsDownListener {

  @Autowired ApArticleConfigService apArticleConfigService;

  @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
  public void receiveMessage(String message) {
    log.info(" 接收到 文章上下架的通知消息, 消息的内容为==>{}",message);
    Map map = JSONUtil.toBean(message, Map.class);

    Long articleId = (Long) map.get("articleId");

    Object enable = map.get("enable");
    // 是否下架,1是上架所以1是false,0就是true
    boolean isDown = !enable.equals(1);
//    boolean isDown =  enable.equals(1)? false:true;
    // 根据接收到的消息,更改articleConfig

    apArticleConfigService.update(
        Wrappers.<ApArticleConfig>lambdaUpdate()
            .set(ApArticleConfig::getIsDown, isDown)
            .eq(ApArticleConfig::getArticleId, articleId));

    //    ApArticleConfig articleConfig = apArticleConfigService.getById(articleId);
    //
    //    if (articleConfig==null){
    //      throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
    //    }
    //    articleConfig.setIsDown(isDown);
    //    articleConfig.

  }
}
